package com.tionsy.springbootshiro.utils;

import com.tionsy.springbootshiro.entity.UserEntity;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * @author tiany
 * @date 2018/10/30 17:27
 */
public class PasswordHelper {

    private static final String algorithmName = "md5";
    private static final int hashIterations = 2;

    public static void encryptPassword(UserEntity userEntity) {
        String newPassword = new SimpleHash(
                algorithmName, userEntity.getPassword(), ByteSource.Util.bytes(userEntity.getSalt()), hashIterations
        ).toHex();
        userEntity.setPassword(newPassword);
    }

    public static void main(String[] args) {
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername("admin");
        userEntity.setPassword("admin");
        userEntity.randomSalt();
        encryptPassword(userEntity);
        System.out.println(userEntity);
    }
}

